[% setvar title Perl should support an interactive mode. %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Perl should support an interactive mode.</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Ariel Scolnicov &lt;ariels+<a href='mailto:perl6@compugen.co.il'>perl6@compugen.co.il</a>&gt;
  Date: 31 Aug 2000
  Last Modified: 22 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number:  184
  Version: 3
  Status: Frozen</pre>
<a name='DISCUSSION'></a><h1>DISCUSSION</h1>
<p>Very little discussion was generated by this RFC.  Several people
noted that <code>perl -de 42</code> and the Perl shell <code>psh</code> already provide
some of what the RFC requests; this is noted in the RFC.</p>
<p>The RFC is not being withdrawn, since 2 other people expressed (mild)
interest in it.</p>
<p>No changes have been made since the last posted version (version 2 of
3 Sep 2000), other than the addition of this &quot;DISCUSSION&quot;.</p>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Perl5 does not have an interactive mode.  The debugger is fine for
testing a single line, but it is inadequate for running a set of
commands interactively.  The Perl6 parser (and possibly the language)
should contain hooks to allow full interactive environments to be
written.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Perl does not have an interactive mode.  It has <code>perl -de 42</code>, but
that is not the same.  An interactive mode is useful not only for a
debugger, but also for exploring the capabilities of a module, or even
for performing simple &quot;one-off&quot; programming tasks.</p>
<p>The most serious obstacle to easy interaction is the difficulty in
typing multiple line commands to a Perl debugger (see below).
However, the Perl debugger also limits this use in other ways, notably
by evaluating each line in a separate <code>eval</code>.  This too has
unfortunate consequences.</p>
<p>Languages which include better interactive capabilities than Perl's
include Python and zsh.</p>
<a name='Example'></a><h2>Example</h2>
<p>Observe an interaction with another language whose name begins with a
`P':</p>
<pre>    Python 1.5.1 (# 1, Jul 28 1998, 22:02:27)  [GCC 2.7.2.3] on sunos5
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    &gt;&gt;&gt; def fact(x):
    ...   if x &lt;= 1:
    ...     return 1
    ...   else:
    ...     return x*fact(x-1)
    ... 
    &gt;&gt;&gt; fact(10)
    3628800
    &gt;&gt;&gt; ^D</pre>
<p>Note in particular the definition of <code>fact</code>, which spans multiple
lines.</p>
<p>With Perl5, it doesn't work:</p>
<pre>    &lt;bioserv 108 [13:31] ~ &gt;perl -de 42

    Loading DB routines from perl5db.pl version 1.0402
    Emacs support available.

    Enter h or `h h' for help.

    main::(-e:1):   42
    DB&lt;1&gt; sub fact {
    Missing right bracket at (eval 5) line 4, at end of line
    syntax error at (eval 5) line 4, at EOF</pre>
<p>[ oops... must fit it all on one line ]</p>
<pre>    &lt; shift; if ($x &lt; 2) { return 1 } else { return $x*fact($x-1) } 

    Missing right bracket at (eval 12) line 4, at end of line
    syntax error at (eval 12) line 4, at EOF</pre>
<p>[ can't see the beginning of the line I'm editing, and forgot a close
brace; might as well forget about any indentation to help remind me ]</p>
<pre>    &lt;if ($x &lt; 2) { return 1 } else { return $x*fact($x-1) } }</pre>
<p>[ Finally!  But I can't even see what I typed! ]</p>
<pre>    DB&lt;4&gt; print fact(10)
    3628800</pre>
<p>Michael Maraist and Tom Christiansen point out that the debugger
allows explicit marking of continuation lines by backslashes:</p>
<pre>    &lt;selena 150 [14:37] ~ &gt;perl -de 42

    Loading DB routines from perl5db.pl version 1.0402
    Emacs support available.

    Enter h or `h h' for help.

    main::(-e:1):   42
    DB&lt;1&gt; sub fact {             \
    cont:   my $x = shift;       \
    cont:   if ($x &lt; 2) {        \
    cont:     return 1           \
    cont:   } else {             \
    cont:     return $x*fact($x-1) \
    cont:   }                    \
    cont: }

    DB&lt;2&gt; x fact 10
    0  3628800</pre>
<p>This is inconvenient.  Syntax in an interactive mode should mirror
normal Perl syntax as far as possible; <code>perldoc perldebug</code> goes so
far as to say</p>
<pre>             Note that this business of escaping a newline is
             specific to interactive commands typed into the
             debugger.</pre>
<a name='Separate eval()s'></a><h2>Separate eval()s</h2>
<p><code>my</code> and <code>local</code> variables don't work in the debugger as one would
expect; their scope does not propagate between lines:</p>
<pre>    &lt;bioserv 112 [14:08] ~ &gt;perl -de 42

    Loading DB routines from perl5db.pl version 1.0402
    Emacs support available.

    Enter h or `h h' for help.

    main::(-e:1):   42
    DB&lt;1&gt; $x = 17

    DB&lt;2&gt; my $x=5 

    DB&lt;3&gt; x $x
    0  17</pre>
<p>The ability to be able to create variables is essential for serious
interactive use of Perl.</p>
<p>What causes all this is that the debugger evaluates every line in a
separate <code>eval</code>; this is not what is desired in an interactive
environment.</p>
<p>This is another limitation on using the debugger for interactive work.
For another example, it is impossible to change packages persistently:</p>
<pre>    DB&lt;3&gt; package foo

    DB&lt;4&gt; $x = 2

    DB&lt;5&gt; x $foo::x
    0  undef
    DB&lt;6&gt; x $x
    0  2
    DB&lt;7&gt; x $main::x
    0  2</pre>
<a name='Possible uses'></a><h2>Possible uses</h2>
<ul>
<li><a name='The Perl debugger (and other Perl debuggers)'></a>The Perl debugger (and other Perl debuggers)</li>
<li><a name='Interaction environments (e.g. perldl)'></a>Interaction environments (e.g. <code>perldl</code>)</li>
<li><a name='&quot;Super&quot; calculators'></a>&quot;Super&quot; calculators</li>
<li><a name='Perl shell'></a>Perl shell</li>
</ul>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>This would require considerable assistance from the parser, I think.
At the very least, it should be possible to feed Perl multi-line input
from the terminal.  Perl should read whole lines, and respond <i>only</i>
when it has parsed a complete statement at the end of a line or when
it has read enough to identify a syntax error.</p>
<p>There are proposals to allow programmatic access to the Perl parser.
Such access might allow &quot;interactive Perl&quot; (<code>iPerl</code>?) to be written
in Perl.</p>
<p>User input will need to be interpreted in a &quot;continuously interrupted&quot;
context of the Perl interpreter.  Use of <code>eval</code> is insufficient, as
the second example show.  Being able to create, run and interrogate a
secondary Perl interpreter (from within Perl) could help.</p>
<p>As much as possible of these interaction environments should be
outside of the core, preferably written in Perl.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<ul>
<li><a name='perldebug'></a>perldebug</li>
<p>manpage for discussion of the debugger</p>
<li><a name='zsh manuals'></a>zsh manuals</li>
<p>(especially zshzle)</p>
<li><a name='zsh'></a>zsh</li>
<p>itself for examples of how multi-line editing can make sense (things
worth stealing)</p>
<li><a name='bash'></a>bash</li>
<p>also lets you type multi-line commands, but flattens them for editing.</p>
<li><a name='psh'></a>psh</li>
<p>(Perl SHell) allows you to type multi-line perl statements, if you
make sure to have an unbalanced syntax element.  (It also does a lot
more, most of which is not relevant to this RFC).
<a href='http://sourceforge.net/projects/psh/' target='_blank'>sourceforge.net</a></p>
<li><a name='python1.5'></a>python1.5</li>
<p>For a useful interactive mode.  <a href='http://www.python.org/.' target='_blank'>www.python.org</a>  See also
chapter 2 of the tutorial
(<a href='http://www.python.org/doc/current/tut/node4.html)' target='_blank'>www.python.org</a> which discusses
this interactive mode.</p>
</ul>
</div>
